import { createSlice } from "@reduxjs/toolkit";
import { setCookie } from "nookies";

export interface UserState {
  token: string | null;
  user: any;
}

const initialState: UserState = {
  token: null, // 选中 nav 数据
  user: null,
};

export const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    setToken: (state, action) => {
      setCookie(null, "token", action.payload, {
        maxAge: 30 * 24 * 60 * 60,
        path: "/",
      });
      state.token = action.payload;
    },

    setUser: (state, action) => {
      state.user = action.payload;
    },
  },
});

export const { setToken, setUser } = userSlice.actions;

export default userSlice.reducer;
